module LocalizedDates
  
  def self.included(base)
    base.extend(ClassMethods)
    base.send(:include, LocalizedDates::InstanceMethods)
  end
  
  module InstanceMethods
    # prepared if we want to add any
  end
  
  module ClassMethods

    DATE_FORMAT = '%d.%m.%Y'
    DATETIME_FORMAT = "%d.%m.%Y %H:%M"
    SUFFIX = "_localized"
    
    def localized_date_fields()
    end
    
    def localized_date_field(name, suffix_name= SUFFIX)
      name_str, name_sym = name.to_s, name.to_sym
      define_method "#{name_str+suffix_name}=" do |input|
        @attributes[name_str] = Date.strptime(input, DATE_FORMAT).to_s rescue nil
      end
      
      define_method name_str+suffix_name do
        return send(name_sym).strftime(DATE_FORMAT) rescue ''
      end
    end
    
    def localized_datetime_field(name, suffix_name= SUFFIX)
      name_str, name_sym = name.to_s, name.to_sym      
      define_method "#{name_str+suffix_name}=" do |input|
        @attributes[name_str] = DateTime.strptime(input, DATETIME_FORMAT).to_s rescue nil
      end

      define_method name_str+suffix_name do
        return send(name_sym).strftime(DATETIME_FORMAT) rescue ''
      end

    end
    
  end

end